package com.example.filestoragewrite;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText editContent, editFilename, editReadContent;
    Button btnWriteInner, btnWriterOuter, btnReadInner, btnReadOuter;
    boolean flag = false;
    FileOutputStream fos = null;
    FileInputStream fis = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editContent = (EditText) findViewById(R.id.editContent);
        editReadContent = (EditText) findViewById(R.id.editReadContent);
        editFilename = (EditText) findViewById(R.id.editFilename);
        btnWriteInner = (Button) findViewById(R.id.btnWriteInner);
        btnWriterOuter = (Button) findViewById(R.id.btnWriteOuter);
        btnWriteInner.setOnClickListener(this);
        btnWriterOuter.setOnClickListener(this);
        btnReadInner = (Button) findViewById(R.id.btnReadInner);
        btnReadOuter = (Button) findViewById(R.id.btnReadOuter);
        btnReadInner.setOnClickListener(this);
        btnReadOuter.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String content = editContent.getText().toString();
        String fileName = editFilename.getText().toString();
        if (content.length() == 0 || fileName.length() == 0) {
            showToast("文本内容或/和文件名为空，请输入内容后重试！");
            return;
        }
        flag = false;
        String readContent="";
        editReadContent.setText("");
        switch (view.getId()) {
            case R.id.btnWriteInner:
                try {
                    fos = openFileOutput(fileName, MODE_PRIVATE);
                    fos.write(content.getBytes());
                } catch (Exception e) {
                    flag = true;
                    e.printStackTrace();
                } finally {
                    try {
                        if (fos != null) {
                            fos.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (flag) {
                        showToast("保存文本到内部存储文件时出错了！");
                    } else {
                        showToast("保存文本到内部存储文件成功！");
                    }
                }
                break;
            case R.id.btnWriteOuter:
                String state = Environment.getExternalStorageState(); //获取外部设备的状态
                if (state.equals(Environment.MEDIA_MOUNTED)) {
                    //File SDPath=Environment.getExternalStorageDirectory();
                    File SDPath = getExternalFilesDir(null);
                    File file = new File(SDPath, fileName);
                    try {
                        fos = new FileOutputStream(file);
                    } catch (Exception e) {
                        e.printStackTrace();
                        flag = true;
                    } finally {
                        try {
                            if (fos != null) {
                                fos.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (flag) {
                        showToast("保存文本到外部存储文件时出错了！");
                    } else {
                        showToast("保存文本到外部存储文件成功！");
                    }
                }
                break;
            case R.id.btnReadInner:
                try {
                    fis = openFileInput(fileName);
                    byte[] buffer = new byte[fis.available()];
                    fis.read(buffer);
                    readContent = new String(buffer);
                } catch (Exception e) {
                    e.printStackTrace();
                    flag = true;
                } finally {
                    try {
                        if (fis != null) {
                            fis.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (flag) {
                    showToast("读取内部存储文件时出错了！");
                } else {
                    editReadContent.setText(content);
                    showToast("读取内部存储文件成功！");
                    editReadContent.setText(readContent);
                }

                break;
            case R.id.btnReadOuter:
                if (sdPath() != null) {
                    File file = new File(sdPath(), fileName);
                    BufferedReader br = null;
                    try {
                        fis = openFileInput(file.getName());
                        br = new BufferedReader(new InputStreamReader(fis));
                        readContent = br.readLine();
                    } catch (Exception e) {
                        e.printStackTrace();
                        flag = true;
                    } finally {
                        if (br != null) {
                            try {
                                br.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                flag = true;
                            }
                        }
                        if (fis != null) {
                            try {
                                fis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                 if(flag){
                  showToast("读取外部存储文件时出错了！");
                 }else{
                  showToast("读取外部存储文件成功！");
                  editReadContent.setText(readContent);
                 }
                }
                break;
        }
    }

    private File sdPath() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            //File SDPath = getExternalFilesDir(null);
            File SDPath=Environment.getExternalStorageDirectory();
            return SDPath;
        } else {
            showToast("未安装SDK！");
            return null;
        }
    }

    private void showToast(String string) {
        Toast.makeText(this, string, Toast.LENGTH_LONG).show();
    }
}
